package com.sky.controller.admin;

import com.sky.dto.CategoryDTO;
import com.sky.dto.CategoryPageQueryDTO;
import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Category;
import com.sky.entity.Dish;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.CategoryService;
import com.sky.service.DishService;
import com.sky.vo.DishOverViewVO;
import com.sky.vo.DishVO;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Set;

@RestController
@RequestMapping("/admin/dish")
@Slf4j
public class DishController {
    @Autowired
    private DishService dishService;
    @Autowired
    private RedisTemplate redisTemplate;

    /**
     * 新增菜品功能
     * @param dishDTO
     * @return
     */
    @CacheEvict(cacheNames = "dishCache",key = "#dishDTO.categoryId")
    @PostMapping
    @ApiOperation("新增菜品功能")
    public Result save(@RequestBody DishDTO dishDTO) {
        log.info("新增菜品功能{}", dishDTO);
        dishService.save(dishDTO);
        String key="dish_"+dishDTO.getCategoryId();
        return Result.success();

    }

    /**
     * 菜品分页展示功能
     * @param dishPageQueryDTO
     * @return
     */
    @GetMapping("/page")
    @ApiOperation("菜品分页展示功能")
    public Result<PageResult> page(DishPageQueryDTO dishPageQueryDTO) {
        log.info("菜品分页功能参数{}", dishPageQueryDTO);
        PageResult pageResult=dishService.pageQuery(dishPageQueryDTO);
        return Result.success(pageResult);

    }
    /**
     * 菜品分页展示功能
     * @param ids
     * @return
     */
    @CacheEvict(cacheNames = "dishCache",allEntries = true)
    @DeleteMapping
    @ApiOperation("批量删除菜品")
    public Result deleteByIds(@RequestParam List<Long> ids) {
        log.info("批量删除菜品，{}", ids);
        dishService.deleteBatch(ids);
        return Result.success();

    }
    /**
     * 菜品分页展示功能
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    @ApiOperation("根据id查找菜品")
    public Result<DishVO> deleteByIds(@PathVariable Long id) {
        log.info("根据查找菜品，{}", id);
        DishVO dishVO =dishService.selectByIdWithFlavors(id);
        return Result.success(dishVO);//dishVO);
    }
    /**
     * 根据菜品分类Id查询菜品
     * @param categoryId
     * @return
     */
    @GetMapping("/list")
    @ApiOperation("根据菜品分类Id查询菜品")
    public Result<List<Dish>> list(Long categoryId) {
        log.info("根据查找菜品，{}", categoryId);
        List<Dish> dishes=dishService.selectByCategoryId(categoryId);
        return Result.success(dishes);//dishVO);
    }
    /**
     * 菜品修改功能
     * @param dishVO
     * @return
     */
    @CacheEvict(cacheNames = "dishCache",allEntries = true)
    @PutMapping
    @ApiOperation("菜品修改功能")
    public Result update(@RequestBody DishVO dishVO) {
        log.info("菜品修改功能，{}", dishVO);
        dishService.update(dishVO);
        return Result.success();
    }

    /**
     * 菜品起售停售功能
     * @param id
     * @return
     */
    /*@CacheEvict(cacheNames = "dishCache",allEntries = true)
    @PostMapping("status/{status}")
    @ApiOperation("菜品起售停售功能")
    public Result deleteByIds(@PathVariable Integer status,Long id) {

        log.info("菜品起售停售功能，{},{}", status,id);
        dishService.startOrStop(status,id);
        return Result.success();
    }*/
    @CacheEvict(cacheNames = "dishCache",allEntries = true)
    @PostMapping("status/{status}")
    @ApiOperation("菜品起售停售功能")
    public Result deleteByIds(@PathVariable Integer status,Long id) {
        log.info("菜品起售停售功能，{},{}", status,id);
        dishService.startOrStop(status,id);
        return Result.success();
    }

    //删除缓存
    private void deleteCache(String pattern){
        Set keys = redisTemplate.keys(pattern);
        redisTemplate.delete(keys);
    }

}
